//
//  UIImage+Resize.m
//  NvSellerShow
//
//  Created by bmd on 2017/3/6.
//  Copyright © 2017年 Meicam. All rights reserved.
//

#import "UIImage+Resize.h"

@implementation UIImage (Resize)

+ (UIImage *)handleImage:(UIImage *)originalImage withSize:(CGSize)size
{//照相机配置为640*480宽度不用裁剪只需裁剪高度即可
    CGFloat w = originalImage.size.width / size.width;//图片与屏幕宽比例

    CGImageRef imgRef = CGImageCreateWithImageInRect([originalImage CGImage], CGRectMake(0, originalImage.size.height/2-size.height*w/2, size.width*w, size.height*w));
    UIImage *newImg = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    return newImg;
}

- (UIImage *)normalizedImage {
    if (self.imageOrientation == UIImageOrientationUp) return self;
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    [self drawInRect:(CGRect){0, 0, self.size}];
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return normalizedImage;
}

+ (UIImage *)handleImage:(UIImage *)originalImage withFrame:(CGRect)rect {
    CGImageRef imgRef = CGImageCreateWithImageInRect([originalImage CGImage], rect);
    UIImage *newImg = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    return newImg;
}

@end
